//
//  BuyListCell.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/27.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "BuyListCell.h"

@interface BuyListCell ()

@property (nonatomic, strong) UIImageView *headView;
@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UIImageView *goodsView;
@property (nonatomic, strong) UILabel *goodsName;
@property (nonatomic, strong) UILabel *goodsNum;
@property (nonatomic, strong) UILabel *goodsPrice;
@property (nonatomic, strong) UILabel *state;
@property (nonatomic, strong) UIButton *left1Btn;
@property (nonatomic, strong) UIButton *left2Btn;
@property (nonatomic, strong) UIButton *rightBtn;

@end

@implementation BuyListCell

+ (BuyListCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"BuyListCell";
    BuyListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[BuyListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = RGB_COLOR(244, 244, 244);
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(211))];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    
    self.headView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(11), ScreenX375(26), ScreenX375(26))];
    self.headView.clipsToBounds = YES;
    self.headView.layer.cornerRadius = ScreenX375(13);
    self.headView.image = ImageWithName(@"头像1");
    [self addSubview:self.headView];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(52), ScreenX375(16), ScreenX375(250), ScreenX375(16))];
    self.nameLabel.textColor = FONTCOLOR_BLACK;
    self.nameLabel.font = Kfont(14);
    self.nameLabel.text = @"南有佳人";
    [self addSubview:self.nameLabel];
    
    self.state = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(317), ScreenX375(17), ScreenX375(50), ScreenX375(15))];
    self.state.textColor = REXADECIMALCOLOR(@"#f21934");
    self.state.font = Kfont(14);
    self.state.text = @"待付款";
    [self addSubview:self.state];
    
    UIView *tLine = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(49), SCREEN_WIDTH - ScreenX375(32), ScreenX375(0.5))];
    tLine.backgroundColor = LineColor;
    [self addSubview:tLine];
    
    self.goodsView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(62), ScreenX375(80), ScreenX375(80))];
    self.goodsView.image = ImageWithName(@"2");
    self.goodsView.contentMode = UIViewContentModeScaleAspectFill;
    self.goodsView.clipsToBounds = YES;
    [self addSubview:self.goodsView];
    
    self.goodsName = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(106), ScreenX375(60), ScreenX375(200), ScreenX375(19))];
    self.goodsName.textColor = FONTCOLOR_BLACK;
    self.goodsName.font = Kfont(15);
    self.goodsName.text = @"印花的设计纯棉纸巾";
    [self addSubview:self.goodsName];
    
    self.goodsNum = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(106), ScreenX375(82), ScreenX375(50), ScreenX375(19))];
    self.goodsNum.textColor = FONTCOLOR_BLACK;
    self.goodsNum.font = Kfont(15);
    self.goodsNum.text = @"x 1";
    [self addSubview:self.goodsNum];
    
    self.goodsPrice = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(106), ScreenX375(124), ScreenX375(150), ScreenX375(18))];
    self.goodsPrice.textColor = FONTCOLOR_BLACK;
    self.goodsPrice.font = Kfont(18);
    self.goodsPrice.text = @"168.80元";
    [self addSubview:self.goodsPrice];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(153.5), SCREEN_WIDTH - ScreenX375(16), ScreenX375(0.5))];
    line.backgroundColor = LineColor;
    [self addSubview:line];
    
    self.left1Btn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(179), ScreenX375(167), ScreenX375(85), ScreenX375(32))];
    [self.left1Btn setTitle:@"联系客服" forState:UIControlStateNormal];
    [self.left1Btn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
    self.left1Btn.layer.borderColor = REXADECIMALCOLOR(@"#eaeaea").CGColor;
    self.left1Btn.layer.borderWidth = 1;
    self.left1Btn.clipsToBounds = YES;
    self.left1Btn.titleLabel.font = Kfont(13);
    self.left1Btn.layer.cornerRadius = ScreenX375(16);
    [self.left1Btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.left1Btn];
    
    self.left2Btn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(80), ScreenX375(167), ScreenX375(85), ScreenX375(32))];
    [self.left2Btn setTitle:@"查看物流" forState:UIControlStateNormal];
    [self.left2Btn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
    self.left2Btn.layer.borderColor = REXADECIMALCOLOR(@"#eaeaea").CGColor;
    self.left2Btn.layer.borderWidth = 1;
    self.left2Btn.clipsToBounds = YES;
    self.left2Btn.titleLabel.font = Kfont(13);
    self.left2Btn.layer.cornerRadius = ScreenX375(16);
    [self.left2Btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.left2Btn];
    
    self.rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(275), ScreenX375(167), ScreenX375(85), ScreenX375(32))];
    [self.rightBtn setTitle:@"立即付款" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:REXADECIMALCOLOR(@"#ffffff") forState:UIControlStateNormal];
    [self.rightBtn setBackgroundImage:ImageWithName(@"按钮背景") forState:UIControlStateNormal];
    self.rightBtn.clipsToBounds = YES;
    self.rightBtn.titleLabel.font = Kfont(13);
    self.rightBtn.layer.cornerRadius = ScreenX375(16);
    [self.rightBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.rightBtn];
}

- (void)buttonAction:(UIButton *)button {
    if ([button.titleLabel.text isEqualToString:@"联系客服"]) {
        if (_chatClick) {
            _chatClick();
        }
    }else if ([button.titleLabel.text isEqualToString:@"查看物流"]) {
        if (_logisticsClick) {
            _logisticsClick();
        }
    }else if ([button.titleLabel.text isEqualToString:@"购买"]) {
        if (_buyClick) {
            _buyClick();
        }
    }else if ([button.titleLabel.text isEqualToString:@"立即付款"]) {
        if (_payClick) {
            _payClick();
        }
    }else if ([button.titleLabel.text isEqualToString:@"确认收货"]) {
        if (_sureClick) {
            _sureClick();
        }
    }else if ([button.titleLabel.text isEqualToString:@"查看链接"]) {
        if (_seeLinkClick) {
            _seeLinkClick();
        }
    }else if ([button.titleLabel.text isEqualToString:@"提醒发货"]) {
        if (_noteClick) {
            _noteClick();
        }
    }
}

- (void)setOrderModel:(OrderModel *)orderModel {
    _orderModel = orderModel;
    
    self.nameLabel.text = orderModel.proUserName;
    [self.headView sd_setImageWithURL:[NSURL URLWithString:orderModel.proUserPic] placeholderImage:nil options:SDWebImageRetryFailed];
    self.goodsName.text = orderModel.proName;
    self.goodsNum.text = [NSString stringWithFormat:@"x%@",orderModel.num];
    [self.goodsView sd_setImageWithURL:[NSURL URLWithString:orderModel.proPic] placeholderImage:nil options:SDWebImageRetryFailed];
    NSString *moneyStr = [NSString stringWithFormat:@"%.2f元",[_orderModel.proPrice floatValue]];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:moneyStr];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:REXADECIMALCOLOR(@"#f21934") range:NSMakeRange(0, [moneyStr length] - 1)];
    self.goodsPrice.attributedText = attributedStr;

    switch ([orderModel.status integerValue]) {
        case 0:
            self.state.text = @"待付款";
            [self.left2Btn setTitle:@"查看物流" forState:UIControlStateNormal];
            [self.rightBtn setTitle:@"立即付款" forState:UIControlStateNormal];
            self.left1Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(196), ScreenX375(167), ScreenX375(85), ScreenX375(32));
            self.rightBtn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(100), ScreenX375(167), ScreenX375(85), ScreenX375(32));
            self.left2Btn.hidden = YES;
            self.rightBtn.hidden = NO;
            break;
        case 1:
            self.state.text = @"待发货";
            [self.left2Btn setTitle:@"提醒发货" forState:UIControlStateNormal];
            self.left1Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(196), ScreenX375(167), ScreenX375(85), ScreenX375(32));
            self.left2Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(100), ScreenX375(167), ScreenX375(85), ScreenX375(32));
            self.left2Btn.hidden = NO;
            self.rightBtn.hidden = YES;
            break;
        case 2:
            self.state.text = @"待收货";
            [self.left2Btn setTitle:@"查看物流" forState:UIControlStateNormal];
            [self.rightBtn setTitle:@"确认收货" forState:UIControlStateNormal];
            self.left1Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(295), ScreenX375(167), ScreenX375(85), ScreenX375(32));
            self.left2Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(196), ScreenX375(167), ScreenX375(85), ScreenX375(32));
            self.rightBtn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(100), ScreenX375(167), ScreenX375(85), ScreenX375(32));
            self.left2Btn.hidden = NO;
            self.rightBtn.hidden = NO;
            break;
        case 5:
            self.state.text = @"已完成";
            if ([_orderModel.proStatus integerValue] == 0) {
                [self.left2Btn setTitle:@"查看链接" forState:UIControlStateNormal];
                self.left1Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(196), ScreenX375(167), ScreenX375(85), ScreenX375(32));
                self.left2Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(100), ScreenX375(167), ScreenX375(85), ScreenX375(32));
                self.left2Btn.hidden = NO;
                self.rightBtn.hidden = YES;
            }else {
                [self.left2Btn setTitle:@"查看物流" forState:UIControlStateNormal];
                [self.rightBtn setTitle:@"购买" forState:UIControlStateNormal];
                self.left1Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(295), ScreenX375(167), ScreenX375(85), ScreenX375(32));
                self.left2Btn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(196), ScreenX375(167), ScreenX375(85), ScreenX375(32));
                self.rightBtn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(100), ScreenX375(167), ScreenX375(85), ScreenX375(32));
                self.left2Btn.hidden = NO;
                self.rightBtn.hidden = NO;
            }
            break;
            
        default:
            break;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@implementation OrderModel

+(JSONKeyMapper*)keyMapper
{
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
                                                                  @"Id": @"id"
                                                                  }];
}


@end

